package com.thunisoft.incremental.contrller;

import java.util.HashMap;
import java.util.Map;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.HttpStatus;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.DeleteMapping;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.PutMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

import com.thunisoft.incremental.common.enums.TaskStatusEnum;
import com.thunisoft.incremental.common.model.Datasource;
import com.thunisoft.incremental.common.model.Group;
import com.thunisoft.incremental.common.model.Task;
import com.thunisoft.incremental.common.util.ControllerUtils;
import com.thunisoft.incremental.contrller.response.Response;
import com.thunisoft.incremental.service.ManageService;

/**
 * ManageController
 *
 * @author Zhang Xin
 * @version v1.0
 * @since 2020/3/27 15:45
 */
@RestController
@RequestMapping("/api/v1/manage")
public class ManageController {

    @Autowired
    private ManageService manageService;



    @GetMapping("datasource")
    public ResponseEntity<Response> getDatasource(){
        return ControllerUtils.getEntity(manageService.getDatasources(),HttpStatus.OK);
    }
    @PostMapping("datasource")
    public ResponseEntity<Response> addDatasource(@RequestBody Datasource datasource){
        try {
            manageService.addDatasource(datasource);
        } catch (Exception e) {
            return ControllerUtils.getEntity(e.getMessage(),HttpStatus.BAD_REQUEST);
        }
        return ControllerUtils.getEntity("ok",HttpStatus.OK);
    }
    @GetMapping("group")
    public ResponseEntity<Response> getGroup(){
        return ControllerUtils.getEntity(manageService.getGroup(),HttpStatus.OK);
    }

    @PostMapping("group")
    public ResponseEntity<Response> addGroup(@RequestBody Group group){
        try {
            manageService.addGroup(group);
        } catch (Exception e) {
            return ControllerUtils.getEntity(e.getMessage(),HttpStatus.BAD_REQUEST);
        }
        return ControllerUtils.getEntity("ok",HttpStatus.OK);
    }

    @GetMapping("task")
    public ResponseEntity<Response> getTask(@RequestParam(value = "groupId",required = false)String groupId
            ,@RequestParam(value = "searchName",required = false)String searchName){
        return ControllerUtils.getEntity(manageService.getTasksByGroup(groupId, searchName),HttpStatus.OK);
    }

    @PostMapping("task")
    public ResponseEntity<Response> addTask(@RequestBody Task task){
        try {
            manageService.addTask(task);
        } catch (Exception e) {
            return ControllerUtils.getEntity(e.getMessage(),HttpStatus.BAD_REQUEST);
        }
        return ControllerUtils.getEntity("ok",HttpStatus.OK);
    }

    @GetMapping("status")
    public ResponseEntity<Response> getStatus(@RequestParam(value = "groupId",required = false)String groupId
            ,@RequestParam(value = "taskId",required = false)String taskId
            ,@RequestParam(value = "status",required = false)Integer status){
        return ControllerUtils.getEntity(manageService.getTaskStatus(groupId, taskId, status),HttpStatus.OK);
    }

    @GetMapping("statusCode")
    public ResponseEntity<Response> getStatusCode(){
        Map<Number,Object> statusCode = new HashMap<>();
        for (TaskStatusEnum value : TaskStatusEnum.values()) {
            statusCode.put(value.getCode(),value.getName());
        }
        return ControllerUtils.getEntity(statusCode,HttpStatus.OK);
    }

    @PutMapping("task/{taskId}")
    public ResponseEntity<Response> exeTask(@PathVariable("taskId") String taskId){
        try {
            manageService.executeByTaskId(taskId);
        } catch (Exception e) {
            return ControllerUtils.getEntity(e.getMessage(),HttpStatus.BAD_REQUEST);
        }
        return ControllerUtils.getEntity("ok",HttpStatus.OK);
    }

    @PutMapping("group/{groupId}")
    public ResponseEntity<Response> exeGroup(@PathVariable("groupId") String groupId){
        try {
            manageService.executeByGroupId(groupId);
        } catch (Exception e) {
            return ControllerUtils.getEntity(e.getMessage(),HttpStatus.BAD_REQUEST);
        }
        return ControllerUtils.getEntity("ok",HttpStatus.OK);
    }

    @PostMapping("testConn")
    public ResponseEntity<Response> testDatabaseConn(@RequestBody Datasource datasource){
        try {
            return ControllerUtils.getEntity(manageService.testDatabaseConnection(datasource),HttpStatus.OK);
        } catch (Exception e) {
            return ControllerUtils.getEntity(e.getMessage(),HttpStatus.OK);
        }
    }

    @DeleteMapping("task/{taskId}")
    public ResponseEntity<Response> deleteTask(@PathVariable("taskId") String taskId){
        manageService.deleteTask(taskId);
        return ControllerUtils.getEntity("删除成功",HttpStatus.OK);
    }

    @GetMapping("checkExistTaskByGroup")
    public ResponseEntity<Response> checkExistTaskByGroup(@RequestParam("groupId") String groupId){
        return ControllerUtils.getEntity(manageService.checkExistTaskByGroup(groupId),HttpStatus.OK);
    }

    @DeleteMapping("group/{groupId}")
    public ResponseEntity<Response> deleteGroupAndTasks(@PathVariable("groupId") String groupId){
        manageService.deleteGroup(groupId);
        return ControllerUtils.getEntity("删除成功",HttpStatus.OK);
    }

    @DeleteMapping("datasource/{datasourceId}")
    public ResponseEntity<Response> deleteDatasource(@PathVariable("datasourceId") String datasourceId){
        try {
            manageService.deleteDataSource(datasourceId);
            return ControllerUtils.getEntity("删除成功",HttpStatus.OK);
        } catch (Exception e) {
            return ControllerUtils.getEntity(e.getMessage(),HttpStatus.FORBIDDEN);
        }

    }

}
